package com.hundsun.yr.universal.p2p.interactor.impl;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.hundsun.yr.universal.library.widget.HSToast;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.listener.BaseMultiLoadedListener;
import com.hundsun.yr.universal.p2p.ui.activity.LoginActivity;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.JSONTool;

/**
 * 数据请求解析
 *
 * @param <T>
 * @author tonglj
 */
public abstract class CommonDataRequestListener<T> implements BaseMultiLoadedListener<String> {
    private Context mContext;
    private BaseActivity mBaseActivity;

    public CommonDataRequestListener(Context context) {
        mContext = context;
        mBaseActivity = (BaseActivity) mContext;
    }

    @Override
    public void onSuccess(String task_name, int event_tag, String data) {
        JSONTool json = new JSONTool(data.toString());
        String errorCode = json.getStringValue("errorCode");
        if (json.isSuccess()) {
            requestSuccess(task_name, event_tag, json.getStringValue(JSONTool.OBJECT));
        } else if (TextUtils.equals(errorCode, Constants.REQUESTCODE_1001)) {//登录
            new HSToast(mContext).showSingletonToast(json.getErrorInfo());
            Intent intent = new Intent(mContext, LoginActivity.class);
            mContext.startActivity(intent);
            mBaseActivity.closeProgress();
        } else {
            new HSToast(mContext).showSingletonToast(json.getErrorInfo());
//			onException(task_name,errorCode);
            mBaseActivity.closeProgress();
        }
    }

    @Override
    public void onException(String task_name, String msg) {
        new HSToast(mContext).showSingletonToast(msg);
        mBaseActivity.closeProgress();
    }

    public abstract void requestSuccess(String task_name, int event_tag, String data);
//	public abstract void onError(String task_name, int event_tag, String errorCode); 

}
